package com.egame.manage.biz.web.controller.pu;

import java.util.*;

import javax.servlet.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import cn.egame.common.servlet.WebUtils;
import cn.egame.common.util.Utils;
import cn.egame.interfaces.EnumType.GameStatus;
import cn.egame.interfaces.EnumType.GameType;
import cn.egame.interfaces.gc.GameInfo;
import cn.egame.interfaces.gc.GameTagLinkInfo;

import com.egame.manage.biz.server.ManageClient;
import com.egame.manage.biz.server.gc.GameServiceImpl;
import com.egame.manage.biz.server.gc.GameTagServiceImpl;
import com.egame.manage.biz.utils.ConstVar;
import com.egame.manage.biz.utils.ConstantVar;
import com.egame.manage.biz.web.controller.BaseController;
import com.lenovo.manager.business.domain.UserBean;

@Controller
@RequestMapping("/store/game/recommend")
public class GameRecommendController extends BaseController  {

	@RequestMapping(value = "/list", method = {RequestMethod.GET,RequestMethod.POST})	
	public ModelAndView toRecommendPage(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		ModelAndView mv = new ModelAndView("/new/op/store/gameRecommendList");
		if(req.getSession().getAttribute(ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int tagid = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId, com.egame.manage.biz.utils.ConstantVar.STORE_GAME_RECOMMEND);
		List<Integer> recommendGids = ManageClient.getInstance().listGameIdByGameTagId(appId,loginUserId,tagid);
		List<GameInfo> recommendtmp = ManageClient.getInstance().listGameInfoByIds(appId, loginUserId, recommendGids);
		List<GameInfo> recommendList = new ArrayList<GameInfo>();
		//对推荐游戏进行排序
		if(recommendtmp != null && recommendtmp.size()>0){
			for(int gid:recommendGids){
				for(GameInfo tmpBean:recommendtmp){
					if(tmpBean != null && tmpBean.getGId() == gid){
						recommendList.add(tmpBean);
					}
				}
			}
		}		
		//过滤推荐游戏
		String recommendName = WebUtils.getString(req, "recommendname","");			
		if(!Utils.stringIsNullOrEmpty(recommendName)){	
			int rid = Utils.toInt(recommendName, 0);
			String rName = rid>0?"":recommendName.trim();
			if(rid>0){
				int i=0;
				while(i<recommendList.size()){
					if(recommendList.get(i).getGId() != rid){
						recommendList.remove(i);
					}else{
						i++;
					}
				}
			}else if(rName.length()>0){
				int i=0;
				while(i<recommendList.size()){
					if(recommendList.get(i).getGameName() != null 
							&& recommendList.get(i).getGameName().indexOf(rName)<0){
						recommendList.remove(i);
					}else{
						i++;
					}
				}
			}			
		}	
		
		String gameName = WebUtils.getString(req, "gname","");
		List<Integer> gids = new ArrayList<Integer>();
		List<GameInfo> gameList = new ArrayList<GameInfo>();		
		if(!Utils.stringIsNullOrEmpty(gameName)){
			int gid = Utils.toInt(gameName, 0);
			String name = gid>0?"":gameName.trim();			
			gids = GameServiceImpl.getInstance().listGIdByTypeAndName(appId, loginUserId, gid, name, GameStatus.business.value(), GameType.shop.value());
			gameList = ManageClient.getInstance().listGameInfoByIds(appId, loginUserId, gids);					
		}	
		//去除推荐游戏	
		gameList.removeAll(recommendList);
		int newTagId = ConstantVar.STORE_TAG_NEW_ID;
		int startTagId = ConstantVar.STORE_TAG_START_ID;
		int recoTagId = ConstantVar.STORE_TAG_RECMMEND_ID;
		
		Map<Integer, GameTagLinkInfo> newMap = new HashMap<Integer, GameTagLinkInfo>();
		GameTagLinkInfo linkBean1 = null;
		for(GameInfo bean:recommendList){
			linkBean1 = ManageClient.getInstance().getGameTagLinkInfoByGId(appId, loginUserId, bean.getGId(), newTagId);
			if(linkBean1 != null){
				newMap.put(bean.getGId(), linkBean1);
			}
		}
		
		Map<Integer, GameTagLinkInfo> startMap = new HashMap<Integer, GameTagLinkInfo>();
		GameTagLinkInfo linkBean2 = null;
		for(GameInfo bean:recommendList){
			linkBean2 = ManageClient.getInstance().getGameTagLinkInfoByGId(appId, loginUserId, bean.getGId(), startTagId);
			if(linkBean2 != null){
				startMap.put(bean.getGId(), linkBean2);
			}
		}
		
		Map<Integer, GameTagLinkInfo> recoMap = new HashMap<Integer, GameTagLinkInfo>();
		GameTagLinkInfo linkBean3 = null;
		for(GameInfo bean:recommendList){
			linkBean3 = ManageClient.getInstance().getGameTagLinkInfoByGId(appId, loginUserId, bean.getGId(), recoTagId);
			if(linkBean3 != null){
				recoMap.put(bean.getGId(), linkBean3);
			}
		}
		mv.addObject("newMap",newMap);
		mv.addObject("startMap",startMap);
		mv.addObject("recoMap",recoMap);
		mv.addObject("recommendList", recommendList);
		mv.addObject("gameList", gameList);
		mv.addObject("gameName", gameName);
		mv.addObject("recommendName", recommendName);
		
		return mv;		
	}	
	
	@RequestMapping(value = "/add", method = {RequestMethod.GET,RequestMethod.POST})	
	public ModelAndView addRecommendGame(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		List<Integer> gids = listGid(req,resp);
		int tagid = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId, ConstVar.STORE_GAME_RECOMMEND);
		for(int gid:gids){
			GameTagServiceImpl.getInstance().addTagLink(appId, loginUserId, gid, tagid, 0, -1);
		}		
		return toRecommendPage(req, resp);
	}

	@RequestMapping(value = "/delete", method = {RequestMethod.GET,RequestMethod.POST})	
	public ModelAndView deleteRecommendGame(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		List<Integer> gids = listGid(req,resp);
		int tagid = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId, ConstVar.STORE_GAME_RECOMMEND);
		for(int gid:gids){
			GameTagServiceImpl.getInstance().deleteTagLink(appId, loginUserId, gid, tagid);
		}		
		return toRecommendPage(req, resp);
	}

	@RequestMapping(value = "/upper", method = {RequestMethod.GET,RequestMethod.POST})	
	public ModelAndView upperRecommendGame(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		int gid = WebUtils.getInt(req, "gid", 0);
		int tagid = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId, ConstVar.STORE_GAME_RECOMMEND);
		GameTagServiceImpl.getInstance().upper(appId, loginUserId, gid, tagid);
		return toRecommendPage(req, resp);
	}

	@RequestMapping(value = "/lower", method = {RequestMethod.GET,RequestMethod.POST})	
	public ModelAndView lowerRecommendGame(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		int gid = WebUtils.getInt(req, "gid", 0);
		int tagid = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId, ConstVar.STORE_GAME_RECOMMEND);
		GameTagServiceImpl.getInstance().lower(appId, loginUserId, gid, tagid);
		return toRecommendPage(req, resp);
	}

	@RequestMapping(value = "/top", method = {RequestMethod.GET,RequestMethod.POST})	
	public ModelAndView topRecommendGame(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		int gid = WebUtils.getInt(req, "gid", 0);
		int tagid = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId, ConstVar.STORE_GAME_RECOMMEND);
		GameTagServiceImpl.getInstance().top(appId, loginUserId, gid, tagid);
		return toRecommendPage(req, resp);
	}

	@RequestMapping(value = "/down", method = {RequestMethod.GET,RequestMethod.POST})	
	public ModelAndView downRecommendGame(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		int gid = WebUtils.getInt(req, "gid", 0);
		int tagid = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId, ConstVar.STORE_GAME_RECOMMEND);
		GameTagServiceImpl.getInstance().down(appId, loginUserId, gid, tagid);
		return toRecommendPage(req, resp);
	}
	
	public List<Integer> listGid(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		List<Integer> gids = new ArrayList<Integer>();
		String gidString = WebUtils.getString(req, "gid", "");
		if(!Utils.stringIsNullOrEmpty(gidString)){
			String[] gidArray = gidString.split(",");
			if(gidArray != null && gidArray.length>0){
				for(String gid : gidArray){
					if(Utils.toInt(gid, 0)>0){
						gids.add(Utils.toInt(gid, 0));
					}
				}
			}
		}
		return gids;
	}
}
